package com.example.canvastest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by 700 on 2018/7/30.
 */

public class DrawCircle extends View{

    private int mMeasureHeigth;
    private int mMeasureWidth;
    private Paint paint;
    private float mCircleXY;
    private float mRadius;

    public DrawCircle (Context context, AttributeSet attrs,
                       int defStyleAttr){
        super(context, attrs, defStyleAttr);
    }

    public DrawCircle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawCircle(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec,
                             int heightMeasureSpec) {
        mMeasureWidth = MeasureSpec.getSize(widthMeasureSpec);
        mMeasureHeigth = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mMeasureWidth, mMeasureHeigth);
        initView();
    }

    private void initView(){
        paint = new Paint();
        paint.setColor(Color.BLUE);
        float length = 0;
        if (mMeasureHeigth >= mMeasureWidth) {
            length = mMeasureWidth;
        } else {
            length = mMeasureHeigth;
        }
        mCircleXY = length / 2;//以手机屏幕中央为圆心
        mRadius = (float) (length * 0.5 / 2);//半径取屏幕宽的1/4
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mCircleXY,mCircleXY,mRadius,paint);
    }
}
